package MySQL;

import back.Product;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class MySQL_Product {
    public boolean addProduct(Product product) {
        String sql = "INSERT INTO product (product_id, name, price, amount) VALUES (?, ?, ?, ?)";
        try(Connection conn = DatabaseConnection.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql)){

            pstmt.setString(1, product.getId());
            pstmt.setString(2, product.getName());
            pstmt.setDouble(3, product.getPrice());
            pstmt.setInt(4, product.getAmount());
            int rows = pstmt.executeUpdate();
            return rows > 0;

        }catch(SQLException e){
            e.printStackTrace();
            return false;
        }
    }
    public boolean updateProduct(Product product) {
        String sql = "UPDATE product SET name = ?, price = ?, amount = ? WHERE product_id = ?";
        try(Connection conn = DatabaseConnection.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql)){
            pstmt.setString(1, product.getName());
            pstmt.setDouble(2, product.getPrice());
            pstmt.setInt(3, product.getAmount());
            pstmt.setString(4, product.getId());
            int rows = pstmt.executeUpdate();
            return rows > 0;

        }catch(SQLException e){
            e.printStackTrace();
            return false;
        }
    }
    public boolean deleteProduct(Product product) {
        String sql = "DELETE FROM product WHERE product_id = ?";
        try(Connection conn = DatabaseConnection.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql)){
            pstmt.setString(1, product.getId());
            int rows = pstmt.executeUpdate();
            return rows > 0;

        }catch(SQLException e){
            e.printStackTrace();
            return false;
        }
    }
    public List<Product> getAllProduct(){
        List<Product> products = new ArrayList<>();
        String sql = "SELECT * FROM product";
        try(Connection conn = DatabaseConnection.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql)){

            while(rs.next()){
                Product product = new Product();
                product.setId(rs.getString("product_id"));
                product.setName(rs.getString("name"));
                product.setPrice(rs.getDouble("price"));
                product.setAmount(rs.getInt("amount"));
                products.add(product);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return products;
    }
    public Product getProductByIP(String id){
        Product  product = new Product();
        String sql = "SELECT * FROM product WHERE product_id = ?";
        try(Connection conn = DatabaseConnection.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql)){

            pstmt.setString(1, id);
            ResultSet rs = pstmt.executeQuery();
            if(rs.next()){
                product.setId(rs.getString("product_id"));
                product.setName(rs.getString("name"));
                product.setPrice(rs.getDouble("price"));
                product.setAmount(rs.getInt("amount"));
                return product;
            }else {
                System.out.println("没有" + id);
                return null;
            }

        }catch(SQLException e){
            e.printStackTrace();
            return null;
        }
    }
    public Product getProductByName(String name){
        Product  product = new Product();
        String sql = "SELECT * FROM product WHERE name = ?";
        try(Connection conn = DatabaseConnection.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql)){

            pstmt.setString(1, name);
            ResultSet rs = pstmt.executeQuery();
            if(rs.next()){
                product.setId(rs.getString("product_id"));
                product.setName(rs.getString("name"));
                product.setPrice(rs.getDouble("price"));
                product.setAmount(rs.getInt("amount"));
                return product;
            }else {
                System.out.println("没有" + name);
                return null;
            }

        }catch(SQLException e){
            e.printStackTrace();
            return null;
        }
    }
}
